<template>
  <div class="container">
    <div class="p16">
      <el-form ref="form" :model="form" label-width="110px">
        <div style="width:800px">
          <el-form-item label="标题文字">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
        </div>
        <div style="1200px">
          <el-form-item label="logo图片">
            <div class="img_boxMore">
              <div class="img_box modelBox" v-for="(file,index) in form.urlList" ::key="index">
                <img :src="file.url" alt="" />
                <div class="model">
                  <i class="el-icon-zoom-in" @click="handlePictureCardPreview(file,index)"></i>
                  <i class="el-icon-delete"  @click="handleDeleteUpload(file,index)"></i>
                </div>
              </div>
              <el-upload
                action=""
                list-type="picture-card"
                :file-list="form.urlList"
                :http-request="handleCustomUpload"
                :show-file-list="false"
                :multiple="true"
              >
              <i class="el-icon-plus"></i>
                <template #file="{ file }">
                  <div class="img_box modelBox" >
                    <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                    <div class="model">
                      <i class="el-icon-zoom-in" @click="handlePictureCardPreview"></i>
                      <i class="el-icon-delete"></i>
                      <!-- <el-icon  @click="handlePictureCardPreview(file)"><zoom-in /></el-icon>
                      <el-icon @click="handleRemove(file)" v-if="!disabled"><Delete /></el-icon> -->
                    </div>
                  </div>
                </template>
              </el-upload>
            </div>
          </el-form-item>
        </div>
        <el-form-item>
          <span class="tips">请上传jpg、png格式，尺寸建议为246px*102px</span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-image-viewer
      v-if="isShowImgView"
      :url-list="dialogImageUrl"
      :initial-index="rowIndex"
      :on-close="closeViewer"
      :z-index="3000"
    />
  </div>
</template>
<script>
  import {
    arraySort
  } from "@/utils";
  import naTable from "@/components/naTable";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "Materiel",
    components: {
      "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
    },
    data() {
      return {
        form: {
        },
        urlView:[],
        dialogImageUrl:[],
        isShowImgView:false,
        rowIndex:0
      };
    },
    computed: {
      ...mapGetters(["name"]),
    },
    created() {
    },
    mounted() {
      this.getData()
    },
    methods: {
      getData(){
        this.$api.system.GetList(7).then((res)=>{
        this.form = res[0]
        this.dialogImageUrl = res[0].urlList.map((x)=>{
          return x.url
        })
        })
      },

      handleSave(){
        this.$api.system.Update(this.form).then(res=>{
          this.$message.success('保存成功！')
          this.getData()
        })
      },

       //删除图片
    handleDeleteUpload(row,index){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.form.urlList.splice(index, 1)
          this.dialogImageUrl = this.form.urlList.map((x)=>{
          return x.url
        })
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
        });
      },

      //预览
      handlePictureCardPreview (file,index) {
        this.rowIndex = index
        this.isShowImgView = true
      },

      //关闭预览
      closeViewer(){
        this.isShowImgView = false
      },


      handleCustomUpload(e){
        const file = e.file;
        const type = file.name.substring(file.name.lastIndexOf(".") + 1);
        if (
          type !== "png" &&
          type !== "jpg" &&
          type !== "jpeg" &&
          type !== "gif" &&
          type !== "bmp"
        ) {
          this.$message.error("上传图片格式错误，请重新上传!");
          return false;
        } else {
            var formData = new FormData();
              formData.append("file", file);
              formData.append("output", 'json2');
              this.$api.system.upload(formData).then((res) => {
                
                let prefix = '/assets'
                this.form.urlList.push({
                  url:prefix+res.src,
                  name:res.src.substring(res.src.lastIndexOf('/')+1),
                  isDocument:false,
                })
                this.dialogImageUrl.push(prefix+res.src)
                console.log(this.form.urlList)
                this.$message.success("上传成功！");
              });
        }
      },
    },
  };

</script>
<style lang="scss" scoped>
.container::v-deep{
  $sizeWidth:158px;
  $sizeHeight:100%;
  margin-bottom: 8px;
  .tips{
    color:rgba(153, 153, 153, 1);
  }
  .modelBox{
    .model{
      display: none;
      position: absolute;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.5);
      z-index: 2;
      >*{
        font-size: 20px;
        color: #fff;
        &:hover{
          color: #3f61f1;
        }
        &:not(:last-child){
          margin-right: 8px;
        }
      }
    }
    &:hover{
      .model{
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
  .el-upload{
    width: #{$sizeWidth};
    height: #{$sizeHeight};
    position: relative;
    .tag{
      position: absolute;
      left: 0;
      top: 0;
      padding: 4px 6px;
      line-height: 1.2em;
      background: rgba(0, 0, 0, 0.75);
      color: #fff;
      font-size: 12px;
      border-radius: 6px 0 4px 0;
      z-index: 2;
    }
  }
  .img_box{
    width: #{$sizeWidth};
    height: 150px ;
    position: relative;
    img{
      max-width: 100%;
      height: 100%;
    }
  }
  .img_boxMore{
    display: flex;
    flex-wrap: wrap;
    margin-right: -8px;
    margin-bottom: -8px;
    .img_box{
      border-radius:8px;
      border: 1px #dcdfe6 solid;
      overflow: hidden;
      margin-right: 8px;
    }
    >*{
      margin-right: 8px;
      margin-bottom: 8px;
    }
  }
}

</style>
